package com.example.demo.controller;

import com.example.demo.Response;
import com.example.demo.dto.UserDTO;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class   UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public Response<UserDTO> getUserById(@PathVariable long id) {
        return Response.newSuccess(userService.getUserById(id));
    }
    @GetMapping("/user/name/{name}")
    public Response<UserDTO> getUserByName(@PathVariable String name){
        return Response.newSuccess(userService.getUserByName(name));
    }
    @GetMapping("/user/account/{account}")
    public Response<UserDTO> getUserByAccount(@PathVariable String account){
        return Response.newSuccess(userService.getUserByAccount(account));
    }
    @PostMapping("/user")
    public Response<Long> addNewUser(@RequestBody UserDTO userDTO) {
        return Response.newSuccess(userService.addNewUser(userDTO));
    }

    @DeleteMapping("/user/{id}")
    public void deleteUserById(@PathVariable long id) {
        userService.deleteUserById(id);
    }

    @PutMapping("/user/{id}")
    public Response<UserDTO> updateUserById(@PathVariable long id, @RequestParam(required = false) String name, @RequestParam(required = false) String password, @RequestParam(required = false) String email, @RequestParam(required = false) String phoneNumber,@RequestParam(required = false) String account) {
        return Response.newSuccess(userService.updateUserById(id,name,password,email,phoneNumber,account));
    }
}
